
/*******************************************************************************
 * Smooths time-density curve.
 ******************************************************************************/

#include <iostream>
#include <vector>

using namespace std;

/**
 * Does not change time values (in tv param), only density values (in dv).
 */
void smooth ( vector< double >& tv, vector< double >& dv, int windowsize )
{
  // error check  TODO: exception handling
  if ( tv.size() != dv.size() )
  {
    return;
  }
  
  // process data
  vector< double > outdata;
  double left_edge = dv[ 0 ];
  double right_edge = dv[ dv.size() - 1 ];
  int radius = windowsize / 2;
  for ( int i = 0; i < dv.size(); ++ i )
  {
    double sum = 0;
    for ( int offset = -radius; offset <= radius; ++ offset )
    {
      int ii = i + offset;
      if ( ii >= 0 && ii < dv.size() )
      {
        sum += dv[ i + offset ];
      }
      else if ( ii < 0 )
      {
        sum += left_edge;
      }
      else
      {
        sum += right_edge;
      }
    }
    outdata.push_back( sum / ( 2 * radius + 1 ) );
  }
  
  // copy smooth values back in dv
  for ( int i = 0; i < dv.size(); ++ i )
  {
    dv[ i ] = outdata[ i ];
  }
  
  return;
}
